import 'package:app/pages/timesheet_module/timesheet_model.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

class TimeSheetController extends GetxController{


  var page = 1;

  List dataArr = [];

  EasyRefreshController easyRefreshController = EasyRefreshController();


  var searchTextEditingController = TextEditingController();

  requestData({var search=''})async{

    var params = {
      'page':page,
      'search':search,
      'limit':10,
      'is_me':'1'  // 1-我的（默认）
    };
    var json = await DioManager().kkRequest(Address.timeSheetList,params: params,method: 'get');

    TimeSheetModel model = TimeSheetModel.fromJson(json);

    if(page == 1){
      easyRefreshController.resetLoadState();
      dataArr.clear();
    }
    if(model.data!=null){
      dataArr.addAll(model.data!);
    }else if(model.data == null){
      BotToast.showText(text: '暂无更多');
      easyRefreshController.finishLoad(noMore: true);
    }

    update();
  }

  /// 删除工时单
  requestDataWithUnlink(var id)async{
    var params = {
      'account_analytic_line_id':'${id}',
    };
    var json = await DioManager().kkRequest(Address.unlinkTimeSheet,bodyParams: params);
    if(json['errmsg']=='ok'){

    }
    BotToast.showText(text: json['message']);
  }


  onRefresh()async{
    page = 1;
    requestData();
  }
  onLoad()async{
    page++;
    requestData();
  }




  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData();
  }
}